﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapTXTKON(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveTXTKON(DataRow dwData)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;

            setResult = QueryTXTKON(dwData, 4);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapTXTKON(ref dwLocal, dwData);
            if (!flgExist)
            {
                InsertTXTKON(dwLocal);
            }
            UpdateTXTKON(dwLocal);
        }
        public void InsertTXTKON(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO TXTKON(TXTKSL, TXTKFILLER, TXTKMANDANT, TXTKPIND, TXTKPOS, TXTKATR, TXTKTEXT)"
                + " VALUES (1, '  ', :TXTKMANDANT, :TXTKPIND, :TXTKPOS, 0,:TXTKTEXT)";
            or1.Parameters.Add("TXTKMANDANT", OracleType.NVarChar).Value = dwData["TXTKMANDANT"];
            or1.Parameters.Add("TXTKPIND", OracleType.NVarChar).Value = dwData["TXTKPIND"];
            or1.Parameters.Add("TXTKPOS", OracleType.NVarChar).Value = dwData["TXTKPOS"];
            or1.Parameters.Add("TXTKTEXT", OracleType.NVarChar).Value = dwData["TXTKTEXT"];
            Connect2Proxy(or1);
        }
        public DataSet QueryTXTKON(DataRow dwInput, int option = 1)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbLocal = null;

            or1 = OracleDB.CreateCommand();
            string key = "";
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT TXTISIND AS TXTISIND,TXTITXTART,TXTIPIND,TXTKPOS,TXTKTEXT,TXTIAENDBEN,TXTIAENDDAT"
                        + " FROM TXTIND T JOIN TXTKON ON TXTIMANDANT=TXTKMANDANT AND  TXTIPIND=TXTKPIND"
                        + " WHERE TXTIMANDANT=:TXTIMANDANT"
                       + " AND TXTISIND=:TXTISIND AND TXTITXTART=:TXTITXTART"
                        + " ORDER BY 1,2,3,4";
                    or1.Parameters.Add("TXTIMANDANT", OracleType.NVarChar).Value = dwInput["TXTIMANDANT"];
                    or1.Parameters.Add("TXTITXTART", OracleType.NVarChar).Value = dwInput["TXTITXTART"];
                    or1.Parameters.Add("TXTISIND", OracleType.NVarChar).Value = dwInput["TXTISIND"];
                    break;
                case 2:
                    key = dwInput["TXTISIND"].ToString();
                    or1.CommandText = "SELECT TXTISIND AS TXTISIND,TXTITXTART,TXTIPIND,TXTKPOS,TXTKTEXT,TXTIAENDBEN,TXTIAENDDAT"
                        + " FROM TXTIND T JOIN TXTKON ON TXTIMANDANT=TXTKMANDANT AND  TXTIPIND=TXTKPIND"
                        + " WHERE TXTIMANDANT=:TXTIMANDANT"
                       + " AND TXTISIND LIKE '" + key + "%' AND TXTITXTART=:TXTITXTART"
                        + " ORDER BY 1,2,3,4";
                    or1.Parameters.Add("TXTIMANDANT", OracleType.NVarChar).Value = dwInput["TXTIMANDANT"];
                    or1.Parameters.Add("TXTITXTART", OracleType.NVarChar).Value = dwInput["TXTITXTART"];
                    break;
                case 3:
                    or1.CommandText = "SELECT DISTINCT TXTISIND AS TXTISIND,TXTITXTART,TXTIPIND,TXTKPOS,TXTKTEXT,TXTIAENDBEN,TXTIAENDDAT FROM TXTIND T JOIN VKPOS "
                    + " P ON T.TXTIMANDANT=VKPMANDANT AND T.TXTITXTART='VAD' AND SUBSTR(T.TXTISIND,1,29)=VKPKOLL||VKPART"
                    + " AND TXTISPRACHKZ='  ' JOIN TXTKON K ON TXTIMANDANT=TXTKMANDANT AND  TXTIPIND=TXTKPIND"
                    + " WHERE VKPMANDANT=:VKPMANDANT AND VKPSA <>'AJ' AND P.VKPAUFKRS=:VKPAUFKRS AND P.VKPAUFLFD=:VKPAUFLFD  "
                    + " ORDER BY 1,2,3,4";
                    or1.Parameters.Add("VKPMANDANT", OracleType.NVarChar).Value = dwInput["VKPMANDANT"];
                    or1.Parameters.Add("VKPAUFKRS", OracleType.NVarChar).Value = dwInput["VKPAUFKRS"];
                    or1.Parameters.Add("VKPAUFLFD", OracleType.NVarChar).Value = dwInput["VKPAUFLFD"];
                    break;
                case 4:
                    //           + "WHERE  TXTKMANDANT=:TXTKMANDANT AND TXTKPIND=:TXTKPIND AND TXTKPOS=:TXTKPOS";
                    or1.CommandText = "SELECT * FROM TXTKON "
                        + " WHERE TXTKMANDANT=:TXTKMANDANT AND  TXTKPIND=:TXTKPIND AND TXTKPOS=:TXTKPOS";
                    or1.Parameters.Add("TXTKMANDANT", OracleType.NVarChar).Value = dwInput["TXTKMANDANT"];
                    or1.Parameters.Add("TXTKPIND", OracleType.NVarChar).Value = dwInput["TXTKPIND"];
                    or1.Parameters.Add("TXTKPOS", OracleType.NVarChar).Value = dwInput["TXTKPOS"];
                    break;
                default:
                    break;
            }
            SResult = Connect2Proxy(or1);
            if (SResult.Tables.Count > 0)
            {
                tbLocal = SResult.Tables[0];
                tbLocal.TableName = "TXTKON";
            }
            SReturn.Merge(SResult);


            return SReturn;
        }
        public void UpdateTXTKON(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE TXTKON SET "
            + "TXTKSL=:TXTKSL,"
            + "TXTKFILLER=:TXTKFILLER,"
            + "TXTKATR=:TXTKATR,"
            + "TXTKTEXT=:TXTKTEXT "
            + "WHERE  TXTKMANDANT=:TXTKMANDANT AND TXTKPIND=:TXTKPIND AND TXTKPOS=:TXTKPOS";
            or1.Parameters.Add("TXTKSL", OracleType.NVarChar).Value = dwData["TXTKSL"].ToString();
            or1.Parameters.Add("TXTKFILLER", OracleType.NVarChar).Value = dwData["TXTKFILLER"].ToString();
            or1.Parameters.Add("TXTKMANDANT", OracleType.NVarChar).Value = dwData["TXTKMANDANT"].ToString();
            or1.Parameters.Add("TXTKPIND", OracleType.NVarChar).Value = dwData["TXTKPIND"].ToString();
            or1.Parameters.Add("TXTKPOS", OracleType.NVarChar).Value = dwData["TXTKPOS"].ToString();
            or1.Parameters.Add("TXTKATR", OracleType.NVarChar).Value = dwData["TXTKATR"].ToString();
            or1.Parameters.Add("TXTKTEXT", OracleType.NVarChar).Value = dwData["TXTKTEXT"].ToString();
            Connect2Proxy(or1);
        }
    }
}
